﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

public partial class Department_Administrator_Page : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int state_ID = Int32.Parse(Request.QueryString["state_ID"]);  //Sets the STATE ID Number (used to query the Database) to the QueryString that was sent
        string dept = Request.QueryString["dept"];
        //CreateDS(dept, state_ID);
    }

    private void CreateDS(string dept_name, int state_id)
    {
        string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|Database.accdb;Persist Security Info=True";
        //string selectState = "Select * from Users where (Users.[User_ID] =" + uname + ")";
        //"SELECT Users.*, Organisation.State, Organisation.Org_ID, Teams.Team_ID FROM (Organisation INNER JOIN Teams ON Organisation.Org_ID = Teams.[Organisation Name]) INNER JOIN Users ON Teams.Team_ID = Users.Team_ID where (Users.[User_ID] =" + uname + ")";
        string selectState = "SELECT DISTINCT Jobs.*, Assignments.*, Teams.*, Organisation.Organisation_Name, Incidents.State FROM Organisation INNER JOIN (Incidents INNER JOIN ((Assignments INNER JOIN Jobs ON Assignments.[Job ID] = Jobs.Job_ID) INNER JOIN Teams ON Assignments.[Assigned Team] = Teams.Team_ID) ON Incidents.Incident_ID = Jobs.Incident_ID) ON Organisation.Org_ID = Teams.[Organisation Name] WHERE (((Organisation.Organisation_Name)='"+dept_name+"') AND ((Incidents.State)="+state_id+"))";



        DataSet userDS = new DataSet();
        OleDbConnection db_conn = new OleDbConnection(connString);
        db_conn.Open();
        OleDbDataAdapter db_dataAdapter = new OleDbDataAdapter(selectState, db_conn);
        db_dataAdapter.Fill(userDS);
        db_conn.Close();
        big_Gridview.DataSource = userDS;
        big_Gridview.DataBind();

    }

    protected void available_db(object sender, EventArgs e)
    {
        string count = available_gv.Rows.Count.ToString();
        available_count_lbl.Text = count;
    }

    protected void working_db(object sender, EventArgs e)
    {
        string count = working_gv.Rows.Count.ToString();
        working_count_lbl.Text = count;
    }

    protected void resting_db(object sender, EventArgs e)
    {
        string count = resting_gv.Rows.Count.ToString();
        resting_count_lbl.Text = count;
    }


    protected void big_Gridview_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = big_Gridview.SelectedRow;
        int value = Int32.Parse(row.Cells[4].Text);
        Response.Redirect("~/single_job.aspx?job_id=" + value + "&state_ID=" + Int32.Parse(Request.QueryString["state_ID"]) + "&dept=" + Request.QueryString["dept"]);
    }
    protected void working_teams_ds_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {

    }

    protected void check_update_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in resting_gv.Rows)
        {
            string team_name = row.Cells[0].Text;
            DateTime dt_rested_started = DateTime.Parse(row.Cells[3].Text);
            DateTime dt_now = DateTime.Parse("#" + DateTime.Now.ToShortDateString() + "#");
            TimeSpan time_elapsed = dt_now - dt_rested_started;
            int days_int = time_elapsed.Days;
            int days_allowed = 3;
            if (days_int >= days_allowed)
            {
                string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|Database.accdb;Persist Security Info=True";
                OleDbConnection db_conn = new OleDbConnection(connString);
                OleDbCommand updateCom = new OleDbCommand("UPDATE Teams SET [Resting] = ?, [Date last rested] = ? WHERE (TeamName = ?)", db_conn);
                updateCom.Parameters.Add("Resting", false);
                updateCom.Parameters.Add("Date Resolved", dt_now);
                updateCom.Parameters.Add("Teamzname", team_name);
                db_conn.Open();
                updateCom.ExecuteNonQuery(); //executes the paramaters added depending on the conditions of the if statement
                db_conn.Close();
            }
        }


        Response.Redirect("~/Department Administrator Page.aspx?state_id=" + Request.QueryString["state_id"] + "&dept=" + Request.QueryString["dept"]);
    
    }
}